/*
 Source: ufxTrader.java
 Software Copyright: Hundsun Technologies Inc.
 System: UFX Security Demo (Asynchronous)
 Module: Package input parameter(Trading realated)
 Describition:
 This class is packing trading related input parameters for each function and send the package to the broker, the documents for function 
 description and Java programming helper can be found at ufx website: https://ufx.hscloud.cn/ufx/html/doc/index.html
 Function id include: 333002,333101,333017,333102,333104,332255
 author: Kai Zhan
 Date:2016/03/29
 Others:
 This demo is a sample for using Hundsun UFX System, we are highly not recommand any user to use this demo at any real market，for more details 
 please read <T2SDK JAVA Developer Manual>
 */
package demo.ufx.trade;

import com.hundsun.libsecurity.CryptUtil;
import com.hundsun.t2sdk.common.core.context.ContextUtil;
import com.hundsun.t2sdk.common.share.dataset.DatasetService;
import com.hundsun.t2sdk.impl.client.T2Services;
import com.hundsun.t2sdk.impl.util.ICrypt;
import com.hundsun.t2sdk.interfaces.IClient;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;
import com.hundsun.t2sdk.interfaces.share.dataset.IDatasets;
import com.hundsun.t2sdk.interfaces.share.event.EventTagdef;
import com.hundsun.t2sdk.interfaces.share.event.EventType;
import com.hundsun.t2sdk.interfaces.share.event.IEvent;
import com.hundsun.t2sdk.interfaces.share.exception.DatasetRuntimeException;
import java.io.UnsupportedEncodingException;
import static java.lang.Thread.sleep;
import java.util.logging.Logger;

public class ufxTrade {

    private tradeCallBack callVariable;
    private IClient client = null;
    private T2Services server = T2Services.getInstance(); // 获取T2实例
    private Logger logger = Logger.getLogger("ufxTrade");
    private ICrypt crypt = server.getCrypt(); //加密实例化

    public void Init() throws Exception {
        logger.info("ar_tree");
        //ar_tree是配置在配置文件中的服务
        client = server.getClient("ar_tree"); //走节点ar_tree

    }
//异步登录打包

    public void ReqFunction331100(String account, String password) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("331100", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        //开始打包
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("password", IDataset.DS_BYTE_ARRAY);//密码
        dataset.addColumn("password_type");//密码类别
        dataset.addColumn("input_content");//客户标志类别
        dataset.addColumn("account_content");//输入内容（账号）
        dataset.appendRow();
        dataset.updateString("op_branch_no", "1");//操作分支机构打入值1
        dataset.updateString("op_entrust_way", "7");
        dataset.updateString("op_station", "1");
        byte[] out =new byte[7];
        CryptUtil.HsPwdEncode(password.getBytes(), out);   
        dataset.updateByteArray("password", out);
        //dataset.updateString("password", password); //普通密码打包
        dataset.updateString("password_type", "2");
        dataset.updateString("input_content", "1");
        dataset.updateString("account_content", account);
        event.setServiceId("331100"); //打入功能号
        event.putEventData(dataset); //入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5); //分配节点为5
        unPack(event, event.getServiceId()); //入参解包，同时打印发送包参数信息
        client.send(event); //发送包
    }

    //委托下单
    public void ReqFunction333002(String user_token, String exchange_type, String op_entrust_way, String account, String password, String password_type, String stock_code,
            String entrust_amount, String entrust_price, String entrust_bs, String entrust_prop, String sysnode_id) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("333002", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("exchange_type");//交易类别
        dataset.addColumn("stock_account");//证券账号
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.addColumn("password_type");//密码类别
        dataset.addColumn("stock_code");//证券代码
        dataset.addColumn("entrust_amount");//委托数量
        dataset.addColumn("entrust_price");//委托价格
        dataset.addColumn("entrust_bs");//买卖方向
        dataset.addColumn("entrust_prop");//委托属性
        dataset.appendRow();
        dataset.updateString("user_token", user_token);
        dataset.updateString("exchange_type", exchange_type);
        dataset.updateString("stock_account", "");//证券账号打入空值，系统自动判断
        dataset.updateString("op_entrust_way", op_entrust_way);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        dataset.updateString("password_type", password_type);
        dataset.updateString("stock_code", stock_code);
        dataset.updateString("entrust_amount", entrust_amount);
        dataset.updateString("entrust_price", entrust_price);
        dataset.updateString("entrust_bs", entrust_bs);
        dataset.updateString("entrust_prop", entrust_prop);
        event.setServiceId("333002"); //打入功能号
        //String var=callVariable.sysnode_id();
        //event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5); //分配节点为5
        unPack(event, event.getServiceId()); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //证券委托查询
    public void ReqFunction333101(String client_id, String branch_no, String user_token, String account, String password, String sysnode_id) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("333101", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("branch_no");//分支机构
        dataset.addColumn("client_id");//客户编号
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.addColumn("request_num");//请求行数
        dataset.appendRow();
        dataset.updateString("op_branch_no", "");
        dataset.updateString("op_entrust_way", "7"); //op_entrust_way 打入7 网上交易
        dataset.updateString("op_station", "1");
        dataset.updateString("branch_no", branch_no);
        dataset.updateString("client_id", client_id);
        dataset.updateString("user_token", user_token);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        dataset.updateString("request_num", "1000");
        event.setServiceId("333101");//打入功能号
        event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5); //分配节点为5
        unPack(event, event.getServiceId()); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //委托撤单
    public void ReqFunction333017(String user_token, String branch_no, String client_id, String account, String password, String entrust_no, String sysnode_id) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("333017", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("branch_no");//分支机构
        dataset.addColumn("client_id");//客户编号
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.addColumn("entrust_no");//委托编号
        dataset.appendRow();
        dataset.updateString("op_branch_no", "");
        dataset.updateString("user_token", user_token);
        dataset.updateString("op_entrust_way", "7");
        dataset.updateString("op_station", "1");
        dataset.updateString("branch_no", branch_no);
        dataset.updateString("client_id", client_id);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        dataset.updateString("entrust_no", entrust_no);
        event.setServiceId("333017");//打入功能号
        event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5);//分配节点为5
        unPack(event, event.getServiceId());//入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //证券成交查询
    public void ReqFunction333102(String user_token, String branch_no, String client_id, String account, String password, String sysnode_id) throws Exception {
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("333102", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("branch_no");//分支机构
        dataset.addColumn("client_id");//客户编号
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.addColumn("request_num");//请求行数
        dataset.appendRow();
        dataset.updateString("user_token", user_token);
        dataset.updateString("op_branch_no", "");
        dataset.updateString("op_entrust_way", "7");
        dataset.updateString("op_station", "1");
        dataset.updateString("branch_no", branch_no);
        dataset.updateString("client_id", client_id);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        dataset.updateString("request_num", "1000");
        event.setServiceId("333102");//打入功能号
        event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5);//分配节点为5
        unPack(event, event.getServiceId());//入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //证券持仓查询
    public void ReqFunction333104(String branch_no, String client_id, String user_token, String account, String password, String sysnode_id) throws Exception {
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("333104", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("branch_no");//分支机构
        dataset.addColumn("client_id");//客户编号
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.appendRow();
        dataset.updateString("op_branch_no", "");
        dataset.updateString("op_entrust_way", "7");
        dataset.updateString("op_station", "1");
        dataset.updateString("branch_no", branch_no);
        dataset.updateString("client_id", client_id);
        dataset.updateString("user_token", user_token);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        event.setServiceId("333104");//打入功能号
        event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5);//分配节点为5
        unPack(event, event.getServiceId());//入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //客户资金快速查询
    public void ReqFunction332255(String branch_no, String client_id, String user_token, String account, String password, String sysnode_id) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("332255", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("op_branch_no");//操作分支机构
        dataset.addColumn("op_entrust_way");//委托方式
        dataset.addColumn("op_station");//站点地址
        dataset.addColumn("branch_no");//分支机构
        dataset.addColumn("client_id");//客户编号
        dataset.addColumn("user_token");//用户口令
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("password");//密码
        dataset.appendRow();
        dataset.updateString("op_branch_no", "");
        dataset.updateString("op_entrust_way", "7");
        dataset.updateString("op_station", "1");
        dataset.updateString("branch_no", branch_no);
        dataset.updateString("client_id", client_id);
        dataset.updateString("user_token", user_token);
        dataset.updateString("fund_account", account);
        dataset.updateString("password", password);
        event.setServiceId("332255");//打入功能号
        event.setStringAttributeValue("46", sysnode_id);//打入system node is 到46号域
        event.putEventData(dataset);//入参打包
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 5);//分配节点为5
        unPack(event, event.getServiceId()); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }

    //业务包解包，并打印包
    public void unPack(IEvent event, String functionId) {
        StringBuilder sb = new StringBuilder();
        //获取返回集
        IDatasets result = event.getEventDatas();
        //获取结果集总数
        int datasetCount = result.getDatasetCount();
        //遍历所有的结果集
        for (int i = 0; i < datasetCount; i++) {
            //读取单个结果集信息
            IDataset ds = result.getDataset(0);
            int columnCount = ds.getColumnCount(); //获取数据数量
            //System.out.println(arg0.getServiceId());  
            //打印结果集标题
            for (int j = 1; j <= columnCount; j++) {
                sb.append(ds.getColumnName(j));
                sb.append("|");
            }
            //遍历单个结果集记录，遍历前首先将指针置到开始
            ds.beforeFirst();
            //打印结果集数据
            while (ds.hasNext()) {
                sb.append("\n");
                ds.next();
                for (int j = 1; j <= columnCount; j++) {
                    sb.append(ds.getString(j)).append("|");
                }
            }
        }
        System.out.println("功能号" + functionId + "运行\n以下是相关入参：\n" + sb);//打印入参
    }
}
